package com.fan.configserver.geetest;


import com.fan.configserver.vo.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("config")
public class GeetestController {

    @RequestMapping("geetest")
    public ResponseResult getGeetest(HttpServletRequest httpServletRequest) {
        ResponseResult responseResult = new ResponseResult();
        GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(), GeetestConfig.isnewfailback());
        String resStr = "{}";
        String userid = "test";
        //自定义参数,可选择添加
        HashMap<String, String> param = new HashMap<String, String>();
        param.put("user_id", userid); //网站用户id
        param.put("client_type", "web"); //web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
        param.put("ip_address", "127.0.0.1"); //传输用户请求验证时所携带的IP

        //进行验证预处理
        int gtServerStatus = gtSdk.preProcess(param);

        //将服务器状态设置到session中
        httpServletRequest.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
        int gt_server_status_code = (Integer) httpServletRequest.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);
        String id = httpServletRequest.getSession().getId();
        System.out.println("sessionid:"+id);
        System.out.println(gt_server_status_code);
//            //将userid设置到session中
        httpServletRequest.getSession().setAttribute("userid", userid);

        resStr = gtSdk.getResponseStr();
        responseResult.setData(resStr);
//            PrintWriter out = response.getWriter();
//            out.println(resStr);
        return responseResult;
    }

    @RequestMapping("verifyLogin")
    public ResponseResult verifyLogin(HttpServletRequest httpServletRequest) {
        ResponseResult responseResult = new ResponseResult();
        GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(),
                GeetestConfig.isnewfailback());

        String challenge = httpServletRequest.getParameter(GeetestLib.fn_geetest_challenge);
        String validate = httpServletRequest.getParameter(GeetestLib.fn_geetest_validate);
        String seccode = httpServletRequest.getParameter(GeetestLib.fn_geetest_seccode);

        //从session中获取gt-server状态\
        String id = httpServletRequest.getSession().getId();
        System.out.println("sessionid:"+id);
        Object attribute = httpServletRequest.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);
        System.out.println(attribute);
        int gt_server_status_code = (Integer) attribute;

        //从session中获取userid
        String userid = (String) httpServletRequest.getSession().getAttribute("userid");

        //自定义参数,可选择添加
        HashMap<String, String> param = new HashMap<String, String>();
        param.put("user_id", userid); //网站用户id
        param.put("client_type", "web"); //web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
        param.put("ip_address", "127.0.0.1"); //传输用户请求验证时所携带的IP

        int gtResult = 0;

        if (gt_server_status_code == 1) {
            //gt-server正常，向gt-server进行二次验证

            gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, param);
            System.out.println(gtResult);
        } else {
            // gt-server非正常情况下，进行failback模式验证

            System.out.println("failback:use your own server captcha validate");
            gtResult = gtSdk.failbackValidateRequest(challenge, validate, seccode);
            System.out.println(gtResult);
        }
        Map<String, String> map = new HashMap<>();

        if (gtResult == 1) {
            map.put("status", "success");
            map.put("version", gtSdk.getVersionInfo());
            // 验证成功
//            PrintWriter out = response.getWriter();
//            JSONObject data = new JSONObject();
//            try {
//                data.put("status", "success");
//                data.put("version", gtSdk.getVersionInfo());
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            out.println(data.toString());
        } else {
            // 验证失败
            map.put("status", "fail");
            map.put("version", gtSdk.getVersionInfo());
//            JSONObject data = new JSONObject();
//            try {
//                data.put("status", "fail");
//                data.put("version", gtSdk.getVersionInfo());
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            PrintWriter out = response.getWriter();
//            out.println(data.toString());
        }
        responseResult.setData(map);
        return responseResult;
    }

}
